<template>
  <div class="search-result">
    <van-nav-bar
      fixed
      :title="keyword + '的搜索结果'"
      left-arrow
      @click-left="$router.back()"
    />

    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        @click="$router.push('/detail/' + item.art_id)"
        v-for="(item, index) in list"
        :key="index"
        :title="item.title"
      />
    </van-list>
  </div>
</template>

<script>
import { getSearch } from '@/api/search'

export default {
  name: 'SearchResult',
  computed: {
    keyword() {
      // 获取当前路由上的搜索字
      // console.log(this.$route)
      return this.$route.query.keyword
    }
  },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1
    }
  },
  methods: {
    async onLoad() {
      const res = await getSearch(this.page, this.keyword)
      console.log(res)

      this.list = [...this.list, ...res.data.results]

      // 加载完成。需要将loading置为false
      this.loading = false
      // 加载下一页数据
      this.page++

      if (res.data.results.length === 0) {
        this.finished = true
      }
    }
  }
}
</script>

<style lang="less" scoped>
.van-list {
  padding-top: 46px;
}
</style>
